/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dyndns.freed0m.web.secureauth.Utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author lvallet
 */
public final class HashLogin {

    public static String hash(String username, String password) throws NoSuchAlgorithmException {
        return encode(encode(username)+password);
    }
    private static String encode(String strToEncode)
            throws NoSuchAlgorithmException {
        byte[] hash;
        MessageDigest md = MessageDigest.getInstance("SHA-512");
        hash = md.digest(strToEncode.getBytes());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hash.length; ++i) {
            String hex = Integer.toHexString(hash[i]);
            if (hex.length() == 1) {
                sb.append(0);
                sb.append(hex.charAt(hex.length() - 1));
            } else {
                sb.append(hex.substring(hex.length() - 2));
            }
        }
        return sb.toString();
    }
    private HashLogin(){
    }
}
